/**
 * 
 */
package com.example.demo.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @author 030998
 *
 */

public class SpringBeanUtil implements BeanFactoryPostProcessor, ApplicationContextAware{

	private static ConfigurableListableBeanFactory beanFactory;
//	private static ApplicationContext context = null;
	
	@Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
		SpringBeanUtil.beanFactory = beanFactory;
    }
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if(applicationContext == null) {
//			context = applicationContext;
		}
	}
	
	/**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        if(name == null || beanFactory == null)
            return null;
        return (T) beanFactory.getBean(name);
    }

    /**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> clazz) {
        return beanFactory.getBean(clazz);
    }

}
